package com.yang.frost.blueberry.shiro.filter;

import com.yang.frost.blueberry.shiro.service.LoginRedisService;
import com.yang.frost.blueberry.util.JwtTokenUtil;
import com.yang.frost.blueberry.util.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author 10296
 * @date 2021/4/12
 * @Description: JWT 授权过滤器
 */
public class JwtFilter extends AuthenticatingFilter {


    //    private ShiroLoginService shiroLoginService;

    @Autowired
    private LoginRedisService loginRedisService;

    @Override
    protected AuthenticationToken createToken(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
//        loginRedisService.exists("");
        String token = JwtTokenUtil.getToken();
        if (StringUtils.isBlank(token)) {
            throw new AuthenticationException("token不能为空");
        }
        if (JwtUtil.isExpired(token)) {
            throw new AuthenticationException("JWT Token已过期,token:" + token);
        }

        return null;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        return false;
    }
}
